package com.container.app;

import com.cache.app.Fetcher;
import com.container.app.exceptions.MContainerUncheckedException;
import com.container.app.utilities.ObjectType;

import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;

/**
 * Created with IntelliJ IDEA.
 * User: Georgiana.Scarlat
 * Date: 7/17/12
 * Time: 4:21 PM
 */
final class InstanceFetcher implements Fetcher<Class, CacheableInstance> {
    MContainer container;

    InstanceFetcher(MContainer container) {
        this.container = container;
    }

    public CacheableInstance fetchObject(Class cls) throws MContainerUncheckedException, NoSuchMethodException, InvocationTargetException, InstantiationException, IllegalAccessException {

        CacheableInstance cacheableInstance;
        Object instance;


        Constructor constructor = cls.getDeclaredConstructor();
        constructor.setAccessible(true);
        instance = constructor.newInstance();


        container.resolveDependencies(instance, ObjectType.INSTANCE);

        cacheableInstance = new CacheableInstance(instance);


        return cacheableInstance;
    }


}
